﻿// Copyright (c) Microsoft Corporation.  All rights reserved.
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using EdmMetadataHelpers;
using System.Collections.Generic;
using System.Collections;
using System.Web.SessionState;

namespace EdmMetadataHelpers
{
    /// <summary>
    /// A collection class that holds registered EdmWrapper(s)
    /// </summary>
    public class EdmWrappers: List<EdmWrapper>
    {
        /// <summary>
        /// Used to store the current wrapper if a HttpSession is not available
        /// </summary>
        [ThreadStatic()]
        private static EdmWrappers _current;
        private static bool? _sessionBound;
        /// <summary>
        /// Find the EdmWrapper by name
        /// </summary>
        /// <param name="name">the name of the EdmWrapper to look for</param>
        /// <returns>an EdmWrapper or NULL</returns>
        public EdmWrapper FindByName(string name)
        {
            return this.Where(e => e.Name == name).FirstOrDefault();
        }
        /// <summary>
        /// Find the current EdmWrapperCollection either
        /// via the Session or a threadbound static (_current)
        /// </summary>
        public static EdmWrappers Current
        { 
            get{
                if (SessionBound)
                {
                    return GetSessionBoundCurrent();
                }
                else
                {
                    return GetThreadBoundCurrent();
                }
            }
        }
        /// <summary>
        /// Are we supposed to use the session or a threadbound static?
        /// 
        /// We use Sessions if they are available
        /// </summary>
        private static bool SessionBound
        {
            get {
                if (!_sessionBound.HasValue)
                    _sessionBound = (HttpContext.Current != null);
                return _sessionBound.Value;
            }
        }
        /// <summary>
        /// Get the current from the thread
        /// </summary>
        private static EdmWrappers GetThreadBoundCurrent()
        {
            if (_current == null)
                _current = GetNewCurrent();
            return _current;
        }
        /// <summary>
        /// Get the current from the session
        /// </summary>
        /// <returns></returns>
        private static EdmWrappers GetSessionBoundCurrent()
        {
            EdmWrappers current = HttpContext.Current.Session["EdmMetadataHelps.EdmWrapperCollection.Current"] as EdmWrappers;
            if (current == null)
            {
                current = GetNewCurrent();
                HttpContext.Current.Session["EdmMetadataHelps.EdmWrapperCollection.Current"] = current;
            }
            return current;
        }
        /// <summary>
        /// A method that initializes a new EdmWrappers class
        /// </summary>
        /// <returns></returns>
        private static EdmWrappers GetNewCurrent()
        {
            EdmWrappers collection = new EdmWrappers();

            //collection.Add(new EdmWrapper(
            //    "Northwind",
            //    "System.Data.SqlClient",
            //    @"Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True",
            //    @"C:\schemas\northwind.csdl",
            //    @"C:\schemas\northwind.ssdl",
            //    @"C:\schemas\northwind.msl"));
            return collection;

        }

  
    }
}